::/**
:: * @addtogroup az_gen_dir
:: * az_gen_dir
:: * @{
:: */
::/**
:: * @file        az_gen_dir.bat
:: * @brief       生成组件文件夹
:: * @note        XXXX
:: * @author      靳普诏(puzhao.jin@hopechart.com)
:: * @date        2023/07/28
:: * @version     1.0
::
:: * @par         修改日志
:: * <table>
:: * <tr><th>Date         <th>Version     <th>Author      <th> Description
:: * <tr><td>2023/07/28   <td>1.0         <td>靳普诏       <td> 创建初始版本
:: * @copyright   xxxx
:: */

@echo off
:: ============================= 获取环境参数 ================================

set ARGV_LEN=%#
::echo "There are %ARGV_LEN% arguments passed to the az_gen_dir.bat"

set i=1
:show_argument
if %i% gtr %ARGV_LEN% goto end_show_argument

set arg=!%i%!
echo "Argument %i%: %arg%"

set /a i+=1
goto show_argument

:end_show_argument
echo "All arguments printed"

set SCRIPT_DIR=%~dp0
echo "SCRIPT_DIR=%SCRIPT_DIR%"

:: ============================= 实际调用逻辑 ================================
echo "============= BIGEN: az_gen_dir.bat ============="
echo "[debug] %~1%"

set /p var=input dir_name:

:: TODO 如果传参不是2个则异常退出


set dir_name=%var%
echo "%dir_name%"
::IF "%dir_name%" == "" (
::    echo 错误：缺少参数！
::    echo 使用方法：az_gen_dir <dir_name>
::    exit /b 1
::)



set comp_name=%1
call :checkFolderExistsAndCreate %comp_name%

call :checkFolderExistsAndCreate %comp_name%\build
call :checkFolderExistsAndCreate %comp_name%\doc
call :checkFolderExistsAndCreate %comp_name%\common
call :checkFolderExistsAndCreate %comp_name%\common\include\%comp_name%
call :createOrBackupFile %comp_name%\readme.md

call :checkFolderExistsAndCreate %comp_name%\test\%comp_name%
call :checkFolderExistsAndCreate %comp_name%\test\%comp_name%\doc
call :checkFolderExistsAndCreate %comp_name%\test\%comp_name%\common
call :checkFolderExistsAndCreate %comp_name%\test\%comp_name%\common\include\%comp_name%
call :createOrBackupFile %comp_name%\test\%comp_name%\readme.md

echo "============= END: az_gen_dir.bat ============="
echo "az_gen_dir.bat return errorlevel:%errorlevel%"


REM 定义函数判断文件夹是否存在
:checkFolderExistsAndCreate
set "dest_folder=%1"

REM 使用IF EXIST判断文件夹是否存在
if exist "%dest_folder%" (
    REM echo 文件夹 %dest_folder% 已存在，将忽略。
) else (
    REM 如果不存在，则尝试创建文件夹
    mkdir "%dest_folder%" >nul 2>&1
    if not errorlevel 1 (
        echo 创建文件夹 %dest_folder% 成功。
    ) else (
        echo 创建文件夹 %dest_folder% 失败。强制退出脚本。
        REM exit /b -11
        exit -11
    )
)

REM 函数执行成功，返回成功码 0
exit /b 0


REM 定义函数判断文件是否存在，如果存在则备份旧文件并创建新文件
:createOrBackupFile
set "file=%~1"
set "backup_suffix=.bak"

if exist "%file%" (
    REM 获取当前日期，格式为YYYYMMDD
    for /F "tokens=1-3 delims=/ " %%a in ('date /T') do (
        set "current_date=%%c%%b%%a"
    )

    REM 构建备份文件名，例如 "file.doc.bak20230728"
    set "backup_file=%file%%backup_suffix%%current_date%"

    REM 备份旧文件
    copy "%file%" "%backup_file%" >nul

    REM 创建新文件
    type nul > "%file%"
    echo 创建文件 %file% 成功，旧文件备份为 %backup_file%。
) else (
    REM 如果文件不存在，则直接创建新文件
    type nul > "%file%"
    echo 创建文件 %file% 成功。
)

REM 函数执行成功，返回成功码 0
exit /b 0

pause
:: Generated on "2023-07-28 16:53:06" by the tool "gen_hq_file.py >> V20230509_1" 
